package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Configuration {
	private String url;

	private String driver;

	private String username;

	private String pwd;

	private int initialSize;
	private int maxActive;
	private int maxIdle;
	private int maxWait;
	private String validationQuery;
	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getInitialSize() {
		return initialSize;
	}

	public void setInitialSize(int initialSize) {
		this.initialSize = initialSize;
	}

	public int getMaxActive() {
		return maxActive;
	}

	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}

	public int getMaxIdle() {
		return maxIdle;
	}

	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}

	public int getMaxWait() {
		return maxWait;
	}

	public void setMaxWait(int maxWait) {
		this.maxWait = maxWait;
	}

	public String getValidationQuery() {
		return validationQuery;
	}

	public void setValidationQuery(String validationQuery) {
		this.validationQuery = validationQuery;
	}

	public Configuration(String url, String driver, String username, String pwd) {
		this.url = url;
		this.driver = driver;
		this.username = username;
		this.pwd = pwd;
	}

	public Configuration() {
	}

	/**
	 * 读取配置文件 生成Configuration对象 默认配置文件是类路径中的oracle.xml
	 * 
	 * @throws DocumentException
	 */
	public static Configuration configure() throws Exception {
		try {
			InputStream in = Configuration.class.getResourceAsStream("/db.xml");
			Configuration cfg = load(in);
			return cfg;
		} catch (Exception e) {
			e.printStackTrace();
			return null;//读取失败
		}
	}

	/**
	 * 指定配置文件加载配置信息
	 */
	public static Configuration configure(File configFile) {
		try {
			System.out.println(configFile);
			InputStream in = new FileInputStream(configFile);
			System.out.println(in);
			Configuration cfg = load(in);
			return cfg;
		} catch (Exception e) {
			e.printStackTrace();
			return null;//读取失败
		}
	}

	private static Configuration load(InputStream in) throws Exception {
		SAXReader reader = new SAXReader();
		Document doc = reader.read(in);
		Element jdbc = doc.getRootElement();
		String url = jdbc.element("url").getText();
		String driver = jdbc.element("driver").getText();
		String username = jdbc.element("username").getText();
		String pwd = jdbc.element("password").getText();//若出现不能初始dbutil的错误，在db.xml中增加密码，操作出错提示使用密码后再删除密码
		
		Configuration cfg = new Configuration(url, driver, username, pwd);
		
		Element dbcp = jdbc.element("dbcp");
		cfg.setInitialSize(Integer.parseInt(dbcp.element("initialSize").getText()));
		cfg.setMaxActive(Integer.parseInt(dbcp.element("maxActive").getText()));
		cfg.setMaxIdle(Integer.parseInt(dbcp.element("maxIdle").getText()));
		cfg.setMaxWait(Integer.parseInt(dbcp.element("maxWait").getText()));
		cfg.setValidationQuery(dbcp.element("validationQuery").getText());
		
		return cfg;
	}
	public static void main(String[] args) throws DocumentException {
		Configuration cfg = configure(new File("/home/tarena/0hean/servlet_jsp/empAddDeleteUpdate/db.xml"));
		System.out.println(cfg.getUrl());
		System.out.println(cfg.getDriver());
		System.out.println(cfg.getPwd());
		System.out.println(cfg.getUsername());
		System.out.println("****************");
		System.out.println(cfg.getInitialSize());
		System.out.println(cfg.getMaxActive());
		System.out.println(cfg.getMaxIdle());
		System.out.println(cfg.getMaxWait());
		System.out.println(cfg.getValidationQuery());
	}
}
